"""过期设置"""


from odoo import models, api, fields
from odoo.tools import float_compare
from odoo.exceptions import UserError, ValidationError


class LotConfigSettings(models.TransientModel):
    _inherit = 'res.config.settings'

    # 用于接入WMS
    nd_expire_auto_move = fields.Boolean('是否自动移动即将过期的批次')
    nd_expire_move_location_id = fields.Many2one('stock.location', string="过期自动移动至库位")
    nd_expire_alert_days = fields.Char('过期前提醒天数')
    nd_expire_move_days = fields.Char('过期前移动天数')

    @api.multi
    def set_values(self):
        if self.nd_expire_auto_move and not self.nd_expire_move_location_id:
            raise UserError('如果允许自动移动即将过期的产品,请指定一个目标库位!')
        self.env['ir.config_parameter'].sudo().set_param('nd.expire_auto_move', self.nd_expire_auto_move)
        self.env['ir.config_parameter'].sudo().set_param('nd.nd_expire_alert_days', self.nd_expire_alert_days)
        self.env['ir.config_parameter'].sudo().set_param('nd.nd_expire_move_days', self.nd_expire_move_days)
        self.env['ir.config_parameter'].sudo().set_param('nd.nd_expire_move_location_id', self.nd_expire_move_location_id.id)
        return super().set_values()

    @api.model
    def get_values(self):
        res = super().get_values()
        Parameter = self.env['ir.config_parameter'].sudo()
        res['nd_expire_auto_move'] = Parameter.get_param('nd.expire_auto_move', default=False)
        res['nd_expire_alert_days'] = Parameter.get_param('nd.nd_expire_alert_days', default=False)
        res['nd_expire_move_days'] = Parameter.get_param('nd.nd_expire_move_days', default=False)
        location_id = Parameter.get_param('nd.nd_expire_move_location_id', default=False)
        location_id = location_id and int(location_id) or False
        res['nd_expire_move_location_id'] = location_id
        return res

